/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import estg.dae.projecto.bank.controllers.BankSessionLocal;
import estg.dae.projecto.bank.model.Account;
import estg.dae.projecto.bank.model.Client;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Marco
 */
public class AccountController extends HttpServlet {

    private static String VIEWS_PATH = "/WEB-INF/views/";
    @EJB
    private BankSessionLocal controller;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String actionStr = request.getParameter("action");
        String jspView = "";

        if ("edit".equalsIgnoreCase(actionStr)) {
            jspView = doEdit(request, response);
        } else if ("detail".equalsIgnoreCase(actionStr)) {
            jspView = doDetail(request, response);
        } else if ("new".equalsIgnoreCase(actionStr)) {
            jspView = doNew(request, response);
        } else if ("movements".equalsIgnoreCase(actionStr)) {
            jspView = doMovements(request, response);
        } else if ("save".equalsIgnoreCase(actionStr)) {
            jspView = doSave(request, response);
        } else if ("update".equalsIgnoreCase(actionStr)) {
            jspView = doUpdate(request, response);
        } else {
            jspView = "unauthorized.jsp";
        }

        request.setAttribute("title", "Clientes");

        request.getRequestDispatcher(VIEWS_PATH + jspView).include(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private String doEdit(HttpServletRequest request, HttpServletResponse response) {
        long accountId = Integer.parseInt(request.getParameter("accountId"));
        Account account = controller.getAccountById(accountId);
        Client client = account.getClient();

        request.setAttribute("action", "Editar Conta");
        request.setAttribute("formMethod", "update");
        request.setAttribute("client", client);
        request.setAttribute("account", account);

        return "AccountDetail.jsp";
    }

    private String doDetail(HttpServletRequest request, HttpServletResponse response) {
        long accountId = Integer.parseInt(request.getParameter("accountId"));
        Account account = controller.getAccountById(accountId);
        
        return doDetail(request, response, account);
    }

    private String doDetail(HttpServletRequest request, HttpServletResponse response, Account account) {
        request.setAttribute("action", "Detalhe Conta");
        request.setAttribute("formMethod", "edit");
        request.setAttribute("editable", "disabled");
        request.setAttribute("client", account.getClient());
        request.setAttribute("account", account);

        return "AccountDetail.jsp";
    }

    private String doNew(HttpServletRequest request, HttpServletResponse response) {
        int clientId = Integer.parseInt(request.getParameter("clientId"));
        request.setAttribute("action", "Nova Conta");
        request.setAttribute("formMethod", "save");

        request.setAttribute("client", controller.getClientById(clientId));
        
        return "AccountDetail.jsp";
    }

    private String doSave(HttpServletRequest request, HttpServletResponse response) {
        int clientId = Integer.parseInt(request.getParameter("clientId"));
        int agencyCode = Integer.parseInt(request.getParameter("agency"));
        double overBalance = Double.parseDouble(request.getParameter("overBalance"));
        Client client = controller.getClientById(clientId);
        Account account = controller.createAccount(client, agencyCode, overBalance);

        return doDetail(request, response, account);
    }

    private String doUpdate(HttpServletRequest request, HttpServletResponse response) {
        int accountId = Integer.parseInt(request.getParameter("accountId"));
        Account account = controller.getAccountById(accountId);
        int agencyCode = Integer.parseInt(request.getParameter("agency"));
        double overBalance = Double.parseDouble(request.getParameter("overBalance"));
        account.setAgency(agencyCode);
        account.setOverBalance(overBalance);
        controller.updateAccount(account);

        return doDetail(request, response);
    }

    private String doMovements(HttpServletRequest request, HttpServletResponse response) {
        int accountId = Integer.parseInt(request.getParameter("accountId"));
        Account account = controller.getAccountById(accountId);

         request.setAttribute("client", account.getClient());
         request.setAttribute("account", account);
         request.setAttribute("movements", controller.getAccountMovements(account, 15));

        return "AccountMovements.jsp";
    }
}
